home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 02 modules and namespaces / modulesdemo / module1.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  13.0 KB  |  396 lines

  1. Imports ModulesDemo.Animals.Mammals
  2. Imports Acc = ModulesDemo.Computers.Accessories
  3.  
  4. Imports Microsoft.VisualBasic.Compatibility.VB6
  5.  
  6. Module MainModule
  7.  
  8.     Sub Main()
  9.         ' Run one of the Textxxxx procedures below by uncommenting only one statement
  10.  
  11.         'TestFactorial()
  12.         'TestBasicInheritance()
  13.         'TestImports()
  14.         'TestMultipleDims()
  15.         'TestShorthands()
  16.         'TestCollections()
  17.         'TestBlockVariables()
  18.         'TestDataTypes()
  19.         'TestCollections()
  20.         'TestFixedLenghtStrings()
  21.         'TestInitializers()
  22.         'TestReferenceTypes()
  23.         'TestEnums()
  24.         'TestArrays()
  25.         'TestArrayInitializers()
  26.         'TestArrayCloning()
  27.         'TestArrayLateBinding()
  28.         'TestCompareStructuresAndClasses()
  29.  
  30.         ' You need these statements when running inside Visual Studio, so that
  31.         ' the Console window doesn't disappear
  32.         Console.WriteLine("")
  33.         Console.WriteLine(">>> press Enter to terminate the program <<<")
  34.         Console.ReadLine()
  35.     End Sub
  36.  
  37.     ' this procedure tests the MathFunctions.Factorial function
  38.  
  39.     Sub TestFactorial()
  40.         Console.WriteLine("Factorial(30) = " & CStr(Factorial(30)))
  41.     End Sub
  42.  
  43.     ' this procedure tests the VB.NET collections support the default Item member
  44.  
  45.     Sub TestCollections()
  46.         ' The .NET version of the Collection object 
  47.         Dim col As New Microsoft.VisualBasic.Collection()
  48.         ' Add two elements.
  49.         col.Add("Francesco", "FirstName")
  50.         col.Add("Balena", "LastName")
  51.         ' The following two statements are equivalent (Item is the default member).
  52.         Console.WriteLine(col.Item(1))            ' => Francesco
  53.         Console.WriteLine(col(1))                 ' => Francesco
  54.         ' The following two statements are equivalent (Item is the default member).
  55.         Console.WriteLine(col.Item("LastName"))   ' => Balena
  56.         Console.WriteLine(col("LastName"))        ' => Balena
  57.     End Sub
  58.  
  59.     ' this procedure tests command math shorthands
  60.  
  61.     Sub TestShorthands()
  62.         Dim x As Long = 9
  63.         Dim y As Double = 6.8
  64.  
  65.         ' Increment x by one (same as x = x + 1).
  66.         x += 1
  67.         Console.WriteLine(x)                     ' => 10
  68.         ' Decrement y by two (same as y = y รป 2).
  69.         y -= 2
  70.         Console.WriteLine(y)                     ' => 4.8
  71.  
  72.         ' Double x (same as x = x * 2).
  73.         x *= 2
  74.         Console.WriteLine(x)                     ' => 20
  75.  
  76.         ' Divide x by ten (same as x = x \ 10).
  77.         x \= 10
  78.         Console.WriteLine(x)                     ' => 2
  79.  
  80.         ' Divide y by four (same as y = y / 4).
  81.         y /= 4
  82.         Console.WriteLine(y)                     ' => 1.2
  83.  
  84.         ' Raise y to the 3rd power (same as y = y ^ 3).
  85.         y ^= 3
  86.         Console.WriteLine(y)                     ' => 1.728
  87.  
  88.         Dim s As String = "abc"
  89.         ' Append a constant to a string (same as s = s & "ABC").
  90.         s &= "ABC"
  91.         Console.WriteLine(2)                     ' => abcABC
  92.  
  93.         ' test performance when incrementing an object field
  94.         ' (compile this with overflow checks removed and enabled optimization)
  95.         Dim i As Integer
  96.         Dim obj As New Test()
  97.         Dim start As Date
  98.  
  99.         start = Now
  100.         For i = 1 To 1000000000
  101.             obj.Value += 1
  102.         Next
  103.         Console.WriteLine("obj.Value += 1    {0} secs.", Now.Subtract(start))
  104.  
  105.         start = Now
  106.         For i = 1 To 1000000000
  107.             obj.Value = obj.Value + 1
  108.         Next
  109.         Console.WriteLine("obj.Value = obj.Value + 1    {0} secs.", Now.Subtract(start))
  110.     End Sub
  111.  
  112.  
  113.     ' This procedure tests using a base and an inherited class
  114.  
  115.     Sub TestBasicInheritance()
  116.         ' create an use an object
  117.         Dim aPerson As New Person()
  118.         aPerson.FirstName = "Joe"
  119.         aPerson.LastName = "Doe"
  120.         Console.WriteLine(aPerson.Title & aPerson.CompleteName)    ' => Mr. Joe Doe
  121.  
  122.         ' create and use an inherited object.
  123.         Dim anEmployee As New Employee()
  124.         With anEmployee
  125.             .FirstName = "Robert"
  126.             .LastName = "Smith"
  127.             ' Use the new (non-inherited) members.
  128.             .BirthDate = #2/5/1960#
  129.             Console.WriteLine(.ReverseName)         ' => Smith, Robert
  130.         End With
  131.  
  132.     End Sub
  133.  
  134.     ' this procedure tests the syntax for Imports statements
  135.  
  136.     Sub TestImports()
  137.         Dim m As Mouse      ' same as Animals.Mammals.Mouse
  138.         Dim m2 As Acc.Mouse ' same as Computers.Accessories.Mouse
  139.         ' ...
  140.     End Sub
  141.  
  142.     ' this procedure tests the syntax for declaratins of multiple variables
  143.  
  144.     Sub TestMultipleDims()
  145.         ' Declare three Long variables.
  146.         Dim x, y, z As Long
  147.  
  148.         ' Declare three variables of different type.
  149.         Dim i As Long, k As Integer, s As String
  150.     End Sub
  151.  
  152.     ' this procedure tests block-scoped variables
  153.  
  154.     Sub TestBlockVariables()
  155.         Dim x, z As Integer
  156.  
  157.         ' NOTE: uncomment next line to prove that you can't have a block variable
  158.         ' with the same name as a local variable
  159.         'Dim y As Integer      ' Procedure variable with same name
  160.  
  161.         For x = 1 To 10
  162.             Dim y As Integer    ' A block variable
  163.             ' ...
  164.         Next
  165.  
  166.         ' this block proves that you can have two block variables with same name
  167.         ' provided that their scope isn't nested
  168.         For z = 1 To 10
  169.             Dim y As Integer      ' A block variable
  170.             ' ...
  171.         Next
  172.  
  173.         ' this block proves that inner block variables aren't reinitialized
  174.         ' when the block is reentered.
  175.         For z = 1 To 2
  176.             ' ....
  177.             For x = 1 To 2
  178.                 Dim y As Long
  179.                 y = y + 1
  180.                 Console.Write(y)                ' => 1 2 3 4
  181.                 Console.Write(" ")
  182.             Next
  183.         Next
  184.         Console.WriteLine("")
  185.     End Sub
  186.  
  187.     ' this procedure contains several tests on data types
  188.  
  189.     Sub TestDataTypes()
  190.         Dim x As Integer
  191.  
  192.         ' Increment X if it is less than 100.
  193.         ' (This works also when Option Strict is On.)
  194.         x = x - CInt(x < 100)
  195.         Console.WriteLine(x)                            ' => 1
  196.  
  197.         ' Make it clear that you're adding a Decimal constant.
  198.         Dim d As Decimal
  199.         d = d + 123.45@
  200.  
  201.         Dim ch As Char
  202.         ch = "A"c        ' Note the trailing "c" character.
  203.         ' *** The following line raises a compile error.
  204.         ' ch = "ABC"c      ' More than one character
  205.  
  206.         ch = CChar(Mid("Francesco", 3, 1))
  207.         Console.WriteLine(ch)                           ' => a
  208.  
  209.         ch = Chr(65)    ' This is character "A".
  210.         Console.WriteLine(ch)                           ' => A
  211.     End Sub
  212.  
  213.     ' this procedure tests fixed-length strings
  214.  
  215.     Sub TestFixedLenghtStrings()
  216.         Dim s As New FixedLengthString(10)
  217.         s.Value = "123456789012345"      ' 15 characters
  218.         ' The characters in excess have been truncated.
  219.         Console.WriteLine(s.Value)             ' => 1234567890
  220.     End Sub
  221.  
  222.     ' this procedure tests initializers
  223.  
  224.     Sub TestInitializers()
  225.         ' Two examples of variable initializers
  226.         Dim width As Single = 1000
  227.         Dim firstName As String = "Francesco"
  228.  
  229.         '*** Following line doesn't compile.
  230.         ' Dim x, y, z As Long = 1
  231.  
  232.         ' you can use initializers with non-constant values
  233.         Dim startDate As Date = Now()
  234.  
  235.         ' show that initializers can be used to re-initialize block variables
  236.         Dim x, z As Integer
  237.         For z = 1 To 2
  238.             ' ....
  239.             For x = 1 To 2
  240.                 ' Ensure that the Y variable always starts at zero.
  241.                 Dim y As Long = 0
  242.                 ' ...
  243.             Next
  244.         Next
  245.     End Sub
  246.  
  247.     ' this procedure tests reference types and
  248.     ' proves that Strings are a reference type
  249.  
  250.     Sub TestReferenceTypes()
  251.         ' Person is a class defined in the current application.
  252.         Dim p1 As New Person()
  253.         p1.FirstName = "Francesco"
  254.         ' Assign to another Person variable.
  255.         Dim p2 As Person
  256.         p2 = p1
  257.         ' You can modify the original object through the new variable.
  258.         p2.FirstName = "Joe"
  259.         Console.WriteLine(p1.FirstName)    ' => Joe
  260.  
  261.         ' prove that strings are reference types.
  262.         Dim s1 As String = "Francesco"
  263.         Dim s2 As String = s1
  264.         ' Prove that the two variables point to the same String object.
  265.         Console.WriteLine(s2 Is s1)             ' => True
  266.     End Sub
  267.  
  268.     ' This procedure tests Enum types
  269.  
  270.     Enum Shape
  271.         Triangle        ' This takes a zero value.
  272.         Square          ' 1
  273.         Rectangle       ' 2
  274.         Circle          ' 3
  275.         Unknown = -999  ' (Values don't need to be sorted.)
  276.     End Enum
  277.  
  278.     Sub TestEnums()
  279.         ' A variable that can be assigned an Enum type.
  280.         Dim aShape As Shape = Shape.Square
  281.  
  282.         aShape = CType(1, Shape)
  283.     End Sub
  284.  
  285.     ' this procedure tests array operations
  286.  
  287.     Sub TestArrays()
  288.         ' Declare the array.
  289.         Dim arr() As Integer
  290.         ' ...
  291.         ' Create the array.
  292.         ReDim arr(100)          ' Note that no As clause is used here. 
  293.  
  294.         ' Declare a two-dimensional array.
  295.         Dim arr2(,) As String
  296.         ' Declare a three-dimensional array.
  297.         Dim arr3(,,) As String
  298.         ' ...
  299.         ' Create the arrays.
  300.         ReDim arr2(10, 10)
  301.         ReDim arr3(10, 10, 10)
  302.  
  303.         ReDim Preserve arr2(10, 20)
  304.         ReDim Preserve arr3(10, 10, 20)
  305.  
  306.         '*** The following statements raise an
  307.         '    ArrayTypeMismatchException exception at runtime.
  308.         Try
  309.  
  310.             ReDim Preserve arr2(20, 10)
  311.             ReDim Preserve arr3(10, 20, 20)
  312.         Catch ex As Exception
  313.             Console.WriteLine(ex.Message)
  314.         End Try
  315.     End Sub
  316.  
  317.     ' this procedure tests array initializers
  318.  
  319.     Sub TestArrayInitializers()
  320.         ' Declare and create an array of 5 integers.
  321.         Dim arr() As Integer = {0, 1, 2, 3, 4}
  322.  
  323.         ' Declare and create a two-dimensional array of strings
  324.         ' with two rows and four columns.
  325.         Dim arr2(,) As String = {{"00", "01", "02", "03"}, _
  326.                                  {"10", "11", "12", "13"}}
  327.     End Sub
  328.  
  329.     ' this procedure tests array cloning
  330.  
  331.     Sub TestArrayCloning()
  332.         Dim arr1() As Integer = {0, 111, 222, 333}
  333.         Dim arr2() As Integer
  334.         ' Create another reference to the array.
  335.         arr2 = arr1
  336.         ' Modify the array through the second variable.
  337.         arr2(1) = 9999
  338.         ' Check that the original array has been modified.
  339.         Console.WriteLine(arr1(1))     ' => 9999
  340.  
  341.         Dim arr3() As Integer = {0, 111, 222, 333}
  342.         ' Create a copy (clone) of the array.
  343.         Dim arr4() As Integer = CType(arr1.Clone, Integer())
  344.         ' Modify an element in the new array.
  345.         arr4(1) = 9999
  346.         ' Check that the original array hasn't been affected.
  347.         Console.WriteLine(arr3(1))     ' => 111
  348.  
  349.         ' Copy a 2-dimensional array
  350.         Dim arr5(,) As Integer = {{0, 1, 2, 3}, {0, 10, 20, 30}}
  351.         Dim arr6(,) As Integer
  352.         ' Create a copy of the 2-dimensional array.
  353.         arr6 = CType(arr5.Clone, Integer(,))
  354.         Console.WriteLine(arr6(1, 1))                   ' => 10
  355.  
  356.         ' you can always assign an array of reference types to an array of objects
  357.         Dim strArr() As String = {"00", "11", "22", "33", "44"}
  358.         Dim objArr() As Object = strArr
  359.         Console.WriteLine(objArr(2))       ' => 22
  360.     End Sub
  361.  
  362.     ' this procedure tests the different behavior of Structures and Classes
  363.  
  364.     Sub TestCompareStructuresAndClasses()
  365.         ' Creation is similar, but structures don't require New.
  366.         Dim cPers As New Person()
  367.         Dim sPers As PersonStruct         ' New is optional.
  368.  
  369.         ' Assignment to members is identical.
  370.         cPers.FirstName = "Joe"
  371.         cPers.LastName = "Doe"
  372.         sPers.FirstName = "Joe"
  373.         cPers.LastName = "Doe"
  374.  
  375.         ' Method and property invocation is also identical.
  376.         Console.WriteLine(cPers.CompleteName())         ' => Joe Doe
  377.         Console.WriteLine(sPers.CompleteName())         ' => Joe Doe
  378.  
  379.         ' Assignment to a variable of the same type has different effects.
  380.  
  381.         Dim cPers2 As Person = cPers
  382.         ' Classes are reference types, hence the new variable receives
  383.         ' a pointer to the original object.
  384.         cPers2.FirstName = "Ann"
  385.         ' The original object has been affected.
  386.         Console.WriteLine(cPers.FirstName)    ' => Ann 
  387.         '
  388.         Dim sPers2 As PersonStruct = sPers
  389.         ' Structure are value types, hence the new variable receives
  390.         ' a copy of the original structure.
  391.         sPers2.FirstName = "Ann"
  392.         ' The original structure hasn't been affected
  393.         Console.WriteLine(sPers.FirstName)    ' => Joe
  394.     End Sub
  395. End Module
  396.